# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libavif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.8.1
pkgrel=1
pkgdesc="Library for encoding and decoding .avif files (mingw-w64)"
arch=('any')
url="https://github.com/AOMediaCodec/libavif"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-rav1e"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/AOMediaCodec/libavif/archive/v${pkgver}.tar.gz"
        002-pixbuf-install-repfix.patch
        004-gdk-pixbuf-loader.c-Include-stdlib.h-for-strtol.patch)
sha512sums=('03692fbba49fde08d76cebb847d0404a81113824e9e65edc6a0977f323d060c394d111e2a7647788fa3115f3ae80b0fc92d4fe5be53852e6734b96a8f1c1a078'
            '60f8bad03460695a094a6ffabbe33f839239fef2bae99d9be7f3a3f9cfde14b694d0c68dce6cc987e9c865a0d97638e951d55abcbab4cc554719803e5d0bb2d2'
            '2ead0fe4f51dd66d40cc8a64e874073e6cca8ef1c9f35a7994fb14cce52e7253c8d176e3ca221eebcf4e1acc277d29d0bf8fa155f794d0a369738a7d85e69462')

prepare() {
  cd ${_realname}-${pkgver}

  # https://gitlab.kitware.com/cmake/cmake/-/issues/19632
  patch -p1 -i ${srcdir}/002-pixbuf-install-repfix.patch

  # https://github.com/AOMediaCodec/libavif/pull/295
  patch -p1 -i ${srcdir}/004-gdk-pixbuf-loader.c-Include-stdlib.h-for-strtol.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DAVIF_BUILD_APPS=ON \
    -DAVIF_CODEC_AOM=ON \
    -DAVIF_CODEC_DAV1D=ON \
    -DAVIF_CODEC_RAV1E=ON \
    -DAVIF_BUILD_GDK_PIXBUF=ON \
    -DAVIF_BUILD_TESTS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/README.md ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
